<?php
require_once '../config/database.php';

// JSON 响应函数
function sendJson($data) {
    header('Content-Type: application/json; charset=utf-8');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: Content-Type, Authorization');
    
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}

try {
    $pdo = new PDO(
        'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET,
        DB_USER,
        DB_PASS,
        [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ]
    );
    
    $results = [];
    
    // 修改closure_type字段长度
    try {
        $pdo->exec("ALTER TABLE museum_closure_dates MODIFY COLUMN closure_type VARCHAR(20) NOT NULL DEFAULT 'once'");
        $results[] = "✅ closure_type字段长度已修改为VARCHAR(20)";
    } catch (Exception $e) {
        $results[] = "⚠️ closure_type字段修改失败: " . $e->getMessage();
    }
    
    // 检查表结构
    $stmt = $pdo->query("DESCRIBE museum_closure_dates");
    $columns = $stmt->fetchAll();
    
    sendJson([
        'code' => 200,
        'message' => '数据库修复完成',
        'data' => [
            'results' => $results,
            'table_structure' => $columns
        ]
    ]);
    
} catch (PDOException $e) {
    sendJson([
        'code' => 500,
        'message' => '数据库修复失败: ' . $e->getMessage(),
        'data' => null
    ]);
}
?>